﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class EditUserFrm : BaseFrm
    {
        //所在的部门
        public string Department { get; set; }

        //编辑状态 true增加，false修改
        public bool EditMode { get; set; }

        public TB_User _User{ get; set; }

        #region 检查字符串是否只为数字
        private bool isNumber(string str)
        {
            if (str == "")
                return false;
            int flag = 0;
            char[] c = str.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if (Char.IsNumber(str[i]) || str[i] == '.')
                {
                    flag++;
                }
                else
                {
                    flag = -1;
                    break;
                }
            }
            if (flag > 0)
                return true;
            else
                return false;
        }
        #endregion 

        public int id;

        public int DepId;
        public string DepName;

        /// <summary>
        /// 编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        public EditUserFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
            //Tb_Department = new TB_Department();
            _User = new TB_User();

            BindDepartment();
        }

        private void EditUserFrm_Load(object sender, EventArgs e)
        {

            if (!EditMode)
            {
                //this.uTextBox1.Enabled = false;
                //Tb_Department = this.BaseDataProxy.GetDepartment(Id);
                _User = this.BaseDataProxy.GetUser(this.id);
                if (_User != null)
                {
                    this.Text = "人员信息编辑";
                    this.txtCode.Text = _User.code.ToString();
                    this.txtName.Text = _User.name;
                    this.cbb.Text = _User.sex;
                    this.cbbDepartment.Text = DepName;
                    //this.cbbDepartment.ValueMember = Convert.ToString(DepId);
                    if (_User.isActive)
                        this.cb.Checked = true;
                    else
                        this.cb.Checked = false;

                    this.txtNation.Text = _User.nation;
                    this.txtPosition.Text = _User.position;
                    this.txtTitle.Text = _User.title;
                    this.txtWageGrade.Text = _User.wageGrade.ToString();
                    this.txtPostWage.Text = _User.postWage.ToString();
                    this.txtBaseWage.Text = _User.baseWage.ToString();
                    this.txtMemo.Text = _User.memo;
                    this.txtPwd.Text = _User.pwd;

                    this.txtCode.Enabled = false;
                }
            }
            else
            {
                if (this.codeRule.autoCode)
                {
                    //this.txtCode.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len, !string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill, false);
                    //id = Convert.ToInt32(this.txtCode.Text);
                    this.txtCode.Enabled = false;
                }
                else
                    this.txtCode.Enabled = true;
                this.Text = "新增人员信息";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (EditMode)
            {
                this.txtCode.Text = this.BaseDataProxy.GetSerialNo((int)GRWL.BZSC.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len, !string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill, false);
            }
            _User.id = id;
            _User.code = this.txtCode.Text;
            _User.name = this.txtName.Text;
            //int a = this.cbbDepartment.Text.IndexOf('[');
            //int b = this.cbbDepartment.Text.IndexOf(']');
            //////MessageBox.Show("fdsafdsa[123]".Substring(9, 13-9-1));
            //if (a > 0 && b > a)
            //    _User.department = Convert.ToInt32(this.cbbDepartment.Text.Substring(a + 1, b - a - 1));
            _User.department = Convert.ToInt32(this.cbbDepartment.SelectedValue);
            _User.sex = cbb.Text;
            _User.nation = this.txtNation.Text;
            _User.position = this.txtPosition.Text;
            _User.title = this.txtTitle.Text;
            _User.isActive = this.cb.Checked;

            if (this.txtWageGrade.Text != "")
                _User.wageGrade = Convert.ToInt32(this.txtWageGrade.Text);
            if (this.txtBaseWage.Text != "")
                _User.baseWage = Convert.ToDecimal(this.txtBaseWage.Text);
            if (this.txtPostWage.Text != "")
                _User.postWage = Convert.ToDecimal(this.txtPostWage.Text);
            _User.memo = txtMemo.Text;

            DepName = this.cbbDepartment.Text;

            if (this.txtPwd.Text.Trim() != "")
                _User.pwd = GRWL.BZSC.Common.EncryptCommon.MD5Encrypt(this.txtPwd.Text);//对密码进行加密

            Common.OperateType opType = Common.OperateType.Add;
            try
            {
                if (EditMode)
                {
                    int g = BaseDataProxy.AddUser(_User);
                    if (g > 0)
                    {
                        _User.id = g;
                        MessageBox.Show("已成功保存记录.");
                    }
                }
                else if (!EditMode)
                {
                    opType = Common.OperateType.Update;
                    if (BaseDataProxy.UpdateUser(_User))
                    {
                        MessageBox.Show("修改成功");
                    }
                }
            }
            catch (InvalidCastException eMsg)
            {
                MessageBox.Show(eMsg.Message.ToString());
            }
            XUser xuser = new XUser();
            xuser.User = _User;
            CommonEventArg<XUser> arg = new CommonEventArg<XUser>(xuser, opType);
            GRWL.BZSC.Common.EventCommon.OnUserUdDGVEvnt(sender, arg);
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BindDepartment()
        {
            TB_Department[] tb_Department = this.BaseDataProxy.FindDepartments(null,null,null);

            DataTable dt = tb_Department.ToDataTable();

            this.cbbDepartment.DataSource = dt;
            this.cbbDepartment.DisplayMember = "name";
            this.cbbDepartment.ValueMember = "id";

            //this.cbbDepartment.Items.Add("");

            //Common.BindDeptComboBox(this.cbbDepartment, tb_Department);

            ////Common.BindDeptTree(uTreeView1, tb_Department, "部门列表");
            //for (int i = 0; i < tb_Department.Count(); i++)
            //{
            //    this.cbbDepartment.Items.Add("a");
            //   this.cbbDepartment.Text = tb_Department[i].name;
            //   this.cbbDepartment.Tag = tb_Department[i].id;
            //}
        }

        #region 检查文本数字
        private void txtBaseWage_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.txtBaseWage.Text !="" && !isNumber(this.txtBaseWage.Text))
            {
                MessageBox.Show("请输入数字");
                this.txtBaseWage.Text = "";
                this.txtBaseWage.Focus();
                return;
            }
        }

        private void txtPostWage_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.txtPostWage.Text!="" && !isNumber(this.txtPostWage.Text))
            {
                MessageBox.Show("请输入数字");
                this.txtPostWage.Text = "";
                this.txtPostWage.Focus();
                return;
            }
        }

        private void txtWageGrade_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.txtWageGrade.Text != "" && !isNumber(this.txtWageGrade.Text))
            {
                MessageBox.Show("请输入数字");
                this.txtWageGrade.Text = "";
                this.txtWageGrade.Focus();
                return;
            }
        }
        #endregion

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

    }
}
